package edu.ptu.shortlink.project.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @BelongsProject: short-link
 * @BelongsPackage: edu.ptu.shortlink.admin.config
 * @Author: HYJ
 * @CreateTime: 2025-05-15  01:24
 * @Description: sentinel流控规则配置类
 * @Version: 1.0
 */
@Component
public class SentinelRuleConfig implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        // 创建流控规则列表
        ArrayList<FlowRule> rules = new ArrayList<>();

        // 创建短链接生成接口的流控规则
        FlowRule createOrderRule = new FlowRule();

        // 设置流控规则的属性
        createOrderRule.setResource("create_short-link");
        createOrderRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        createOrderRule.setCount(1);

        rules.add(createOrderRule);
        FlowRuleManager.loadRules(rules);
    }
}
